package net.maergaiyun.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import net.maergaiyun.enums.BizCodeEnum;
import net.maergaiyun.enums.EventMessageType;
import net.maergaiyun.exception.BizException;
import net.maergaiyun.model.EventMessage;
import net.maergaiyun.service.ShortLinkService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@Slf4j
//@RabbitListener(queues = "short_link.add.mapping.queue")
@RabbitListener(queuesToDeclare = {@Queue("short_link.add.mapping.queue")})
public class ShortLinkAddMappingMQListener {
    @Autowired
    private ShortLinkService shortLinkService;

    @RabbitHandler
    public void ShortLinkHandler(EventMessage eventMessage, Message message, Channel channel) throws IOException {
        log.info("监听到创建B端短链消息ShortLinkAddMappingMQListener message消息内容: {}", message);
        // long tag = message.getMessageProperties().getDeliveryTag();
        try {
            eventMessage.setEventMessageType(EventMessageType.SHORT_LINK_ADD_MAPPING.name());
            shortLinkService.handleAddShortLink(eventMessage);

        } catch (Exception e) {
            log.error("创建B端短链消息消费失败: {}", eventMessage);
            throw new BizException(BizCodeEnum.MQ_CONSUME_EXCEPTION);
        }
        log.info("创建B端短链消息消费成功: {}", eventMessage);
        // channel.basicAck(tag, false);
    }
}
